/**
 * Created by Administrator on 2015/9/1.
 */
var userDao = require('../dao/userDao');
var userInfoDao = require('../dao/userInfoDao');
var activeDao = require('../dao/activeDao');
var activeDetailDao = require('../dao/activeDetailDao');
var cusSuperDao = require('../dao/cusSuperDao');
var admitDao = require('../dao/admitDao');


module.exports = {
    add: function (req, res, callback) {
        userDao.add(req, res, function (result) {
            callback(result)
        });
    },
    queryActiveList: function (req, callback) {
        var subject,pages = req.query,pageNo;
        pageNo = parseInt(pages.page) - 1;
        if(pages.page==undefined){
            pageNo=0;
        }
        if(req.param('subject')==undefined){
            subject='';
        }else{
            subject=req.param('subject');
        }
        activeDao.queryAll(req,subject,pageNo, function (result,param) {
            callback(result,param)
        });
    },
    queryActiveDetail: function (req, callback) {
        activeDao.queryById(req, function (result) {
            activeDetailDao.queryById(result[0], function (detailResult) {
                callback(detailResult)
            });
        })
    },
    queryActiveAboutMe: function (req, callback) {
        var subject;
        if(req.param('subject')==undefined){
            subject='';
        }else{
            subject=req.param('subject');
        }
        cusSuperDao.queryActiveByUserInfoId(req, req.session.currentUser.userInfoId,subject, function (result,param) {
            callback(result,param);
        })
    },
    queryComingActive: function (req, callback) {
        var subject;
        if(req.param('subject')==undefined){
            subject='';
        }else{
            subject=req.param('subject');
        }
        cusSuperDao.queryComingActive(req,subject, function (result,param) {
            callback(result,param);
        })
    },
    updatePersonInfo: function (req, res, callback) {
        userInfoDao.updatePersonInfo(req, res, function (result) {
            if (result.affectedRows >= 1 && req.session.currentUser.id == req.body.id) {
                req.session.currentUser.name = req.body.name;
                req.session.currentUser.sex = req.body.sex;
                req.session.currentUser.tel = req.body.tel;
                req.session.currentUser.email = req.body.email;
                req.session.currentUser.birthday = req.body.birthday;
                req.session.currentUser.address = req.body.address;
                result = {
                    code: 200,
                    content: result
                };
                callback(result);

            } else {
                result = {
                    code: 0,
                    content: result
                };
                callback(result);
            }
        });
    },
    queryPassword: function (req, res, callback) {
        userDao.queryPassword(req, res, function (result) {
            callback(result)
        });
    },
    updatePassword: function (req, res, callback) {
        userDao.updatePassword(req, res, function (result) {
            callback(result)
        });
    },
    applyActive: function (req, callback) {
        var userInfoId,activeId,activeDetailId,status=4;
        userInfoId=req.session.currentUser.userInfoId;
        activeId=req.param('activeId');
        activeDetailId=req.param('activeDetailId');
        admitDao.applyActive(req,userInfoId,activeId,activeDetailId,status, function (result,activeDetailId) {
            activeDetailDao.updateRegisNum(req,activeDetailId,function(result){
                callback(result);
            })
        });
    },
    changeAdmitStatus: function (req, callback) {
        var status, id, activeId;
        if (req.param('param') != undefined) {
            var param = req.param('param').split(";");
            status = param[1];
            id = param[3];
            activeId = param[5];
        } else {
            status = req.param('status');
            id = req.param('id');
        }
        cusSuperDao.changeAdmitStatus(req, status, id, function (status, id, result) {
            if (status == 8) {
                activeDetailDao.updateAttendNum(req, activeId, function (result) {
                    var status=8;
                    callback(result,status);
                });
            } else {
                callback(result, status)
            }
        });
    },
    cancelApply: function (req, callback) {
        cusSuperDao.cancelApply(req, function (result) {
            callback(result)
        });
    },
    queryActiveByName: function (req, res, callback) {//ͨ�����������
        activeDao.queryActiveByName(req, res, function (result) {
            callback(result)
        });
    },
    queryTel: function (req, res, callback) {
        userInfoDao.queryTel(req, res, function (result) {
            callback(result)
        });
    },
    queryStatistic:function (req,  callback) {
        activeDetailDao.queryStatistic(req,function (result) {
            if(result.length>0){
                callback(result[0]);
            }else{
                result={
                    code:0,
                    regisNum:0,
                    attendNum:0,
                    inviteNum:0
                }
                callback(result)
            }
        });

    },
}